﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using VietThuongLMS.Services;
using VietThuongLMS.ViewModels;
using VietThuongLMS.Utils;
using DCS.ComLib.Config;
using VietThuongLMS.Areas.Admin.Models;

//Author: Kha
namespace VietThuongLMS.Areas.Admin.Controllers
{
    public class DiscountsController : Controller
    {
        //
        // GET: /Admin/Discounts/

        [Authorize(Roles = "Admin")]
        public ActionResult ChuongTrinhGiamGia()
        {
            int page = 1;
            if (Request["p"] != null)
            {
                page = int.Parse(Request["p"].ToString());
            }
            var dlvm = ChuongTrinhKhuyenMaiService.GetAllDiscountsWithPagination(10, page);
            //dlvm.TinhHocPhiSauKhiGiam();
            //phân trang
            Pagination pagination = new Pagination(true, Url.Content("~"), "Admin", "Discounts", "ChuongTrinhGiamGia", "?p=", dlvm.TotalMoRong, page, 10, "Previous", "Next", "First", "End");
            string pageLinks = pagination.GetPageLinks();
            ViewData["pageLinks"] = pageLinks;
            //end phân trang        
            return View("ChuongTrinhKhuyenMai", dlvm);
        }

        [Authorize(Roles = "Admin")]
        public ActionResult Edit(int id)
        {
            var ctkm = ChuongTrinhKhuyenMaiService.GetChuongTrinhKhuyenMaiByID(id);
            ChuongTrinhKhuyenMaiModel ctkmm = new ChuongTrinhKhuyenMaiModel(ctkm);
            var list = LoaiLopHocService.GetAll();
            ViewData["loailophoc"] = new SelectList(list, "ID", "TenLoaiLopHoc", ctkmm.MaLoaiLopHoc);
            return View("SuaChuongTrinhKhuyenMai", ctkmm);
        }

        [Authorize(Roles = "Admin")]
        public ActionResult Update(ChuongTrinhKhuyenMaiModel ctkmm)
        {
            ChuongTrinhKhuyenMai ctkm = ctkmm.ChuyenDoiModel();
            bool rs = ChuongTrinhKhuyenMaiService.UpdateChuongTrinhKhuyenMai(ctkm);
            return RedirectToAction("ChuongTrinhGiamGia", "Discounts", new { area = "Admin" });
        }

        [Authorize(Roles = "Admin")]
        public ActionResult Create()
        {
            ViewData["loailophoc"] = new SelectList(LoaiLopHocService.GetAll(), "ID", "TenLoaiLopHoc");
            return View("TaoChuongTrinhKhuyenMai");
        }

        [Authorize(Roles = "Admin")]
        public ActionResult DoCreate(ChuongTrinhKhuyenMaiModel ctkmm)
        {
            ChuongTrinhKhuyenMai ctkm = ctkmm.ChuyenDoiModel();
            bool rs = ChuongTrinhKhuyenMaiService.Insert(ctkm);
            return RedirectToAction("ChuongTrinhGiamGia", "Discounts", new { area = "Admin" });
        }

        [Authorize(Roles = "Admin")]
        public ActionResult DoDelete(int id)
        {
            bool rs = ChuongTrinhKhuyenMaiService.UpdateDeleteFiedChuongTrinhKhuyenMai(id);
            return RedirectToAction("ChuongTrinhGiamGia", "Discounts", new { area = "Admin" });
        }
    }
}
